<HTML>
<HEAD>
<TITLE>[Appendix C] C.2 Test Program</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:17:58 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="appc_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Appendix C<br>Platform-Specific Event Handling</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="appd_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-APP-C-SECT-2">C.2 Test Program</A></h2>

<P CLASS=para>
<A NAME="C.COMP"></A>The test program, <tt CLASS=literal>compList</tt>, listed in <A HREF="appc_02.htm#JAWT-APP-C-SECT-2.2">Source Code</A> 
shows the events peers pass along to the Java run-time system. You can 
then examine the output to see how the run-time system reacts to the different 
events. When you run <tt CLASS=literal>compList</tt>, 
the screen looks something like the one in <A HREF="appc_02.htm#JAWT-APP-C-FIG-1">Figure C.1</A>. 

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-APP-C-FIG-1">Figure C.1: Test program</A></h4>


<p>
<img align=middle src="./figs/jawtc01.gif" alt="[Graphic: Figure C-1]" width=214 height=256 border=0>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-APP-C-SECT-2.1">How to Use the Program</A></h3>

<P CLASS=para>
Java does not have an automated record and playback feature, so the work 
is left for you to do. The program displays 10 components: <tt CLASS=literal>Label</tt>, 
<tt CLASS=literal>Button</tt>, <tt CLASS=literal>Scrollbar</tt>, 
<tt CLASS=literal>List</tt>, multiselection <tt CLASS=literal>List</tt>, 
<tt CLASS=literal>Choice</tt>, <tt CLASS=literal>Checkbox</tt>, 
<tt CLASS=literal>TextField</tt>, <tt CLASS=literal>TextArea</tt>, 
and <tt CLASS=literal>Canvas</tt> (the black box in <A HREF="appc_02.htm#JAWT-APP-C-FIG-1">Figure C.1</A>). 
Basically, you must manually trigger every event for every component. 

<P CLASS=para>
For <I CLASS=emphasis>every</I> component on the screen (except Done), do the following:

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry>With the mouse<br>
<DD>

<P CLASS=para>
Move the cursor over the object, press the mouse button 
and release, and drag the cursor over the object. 

<p>
<DT CLASS=varlistentry>With the keyboard<br>
<DD>

<P CLASS=para>
Press and release an alphabetic key, press and release 
the Home and End keys, arrow keys, and function keys. Do this for every 
component, even for components like <tt CLASS=literal>Button</tt> 
and <tt CLASS=literal>Label</tt> that have no logical 
reason for using keyboard events. 

<p>
<DT CLASS=varlistentry>For items with choices<br>
<DD>

<P CLASS=para>
Select and deselect a few choices; double-click and single-click selections. 

<p>
<DT CLASS=varlistentry>For the scrollbar<br>
<DD>

<P CLASS=para>
Click on each arrow, drag the slider, and click in the paging area (the 
space between each arrow and the slider). 

<p>
<DT CLASS=varlistentry>For the text field<br>
<DD>

<P CLASS=para>
Press Enter. 

<p>
<DT CLASS=varlistentry>When finished<br>
<DD>

<P CLASS=para>
Press the Done button, and analyze the results. Run the program 
again (without exiting), and check the results again. Try to trigger any 
specific events that you expect but didn't appear in the output 
from the first pass. Generating some events requires a little work. For 
example, on a Macintosh, in order to get the <tt CLASS=literal>MOUSE_UP</tt> 
and <tt CLASS=literal>MOUSE_DRAG</tt> events, you 
must do a <tt CLASS=literal>MOUSE_DOWN</tt> off the 
component; otherwise, the <tt CLASS=literal>MOUSE_DOWN</tt>/<tt CLASS=literal>MOUSE_UP</tt> 
combination turns into an <tt CLASS=literal>ACTION_EVENT</tt>, 
if that component can generate it. </DL>
<DIV CLASS=note>
<P CLASS=note><BLOCKQUOTE><P><B>NOTE:</B> 
</blockquote><P>
</DIV>

<P CLASS=para>
The SunTest business unit of Sun Microsystems has an early version 
of a record and playback Java GUI testing tool called JavaSTAR. Information 
about it is available at 
<A HREF="http://www.suntest.com/JavaSTAR/JavaSTAR.html">http://www.suntest.com/JavaSTAR/JavaSTAR.html</A>. 
In the future, it may be possible to use JavaSTAR to help automate this 
process. 
</blockquote><P>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-APP-C-SECT-2.2">Source Code</A></h3>

<P CLASS=para>
The following is the source code for the test program:

<DIV CLASS=programlisting>
<P>
<PRE>
import java.awt.*;
import java.util.*;
import java.applet.*;
public class compList extends Applet {
    Button done = new Button ("Done");
    Hashtable values = new Hashtable();
    public void init () {
        add (new Label ("Label"));
        add (new Button ("Button"));
        add (new Scrollbar (Scrollbar.HORIZONTAL, 50, 25, 0, 255));
        List l1 = new List (3, false);
        l1.addItem ("List 1");
        l1.addItem ("List 2");
        l1.addItem ("List 3");
        l1.addItem ("List 4");
        l1.addItem ("List 5");
        add (l1);
        List l2 = new List (3, true);
        l2.addItem ("Multi 1");
        l2.addItem ("Multi 2");
        l2.addItem ("Multi 3");
        l2.addItem ("Multi 4");
        l2.addItem ("Multi 5");
        add (l2);
        Choice c = new Choice ();
        c.addItem ("Choice 1");
        c.addItem ("Choice 2");
        c.addItem ("Choice 3");
        c.addItem ("Choice 4");
        c.addItem ("Choice 5");
        add (c);
        add (new Checkbox ("Checkbox"));
        add (new TextField ("TextField", 10));
        add (new TextArea ("TextArea", 3, 20));
        Canvas c1 = new Canvas ();
        c1.resize (50, 50);
        c1.setBackground (Color.blue);
        add (c1);
        add (done);
    }
    public boolean handleEvent (Event e) {
        if (e.target == done) {
            if (e.id == Event.ACTION_EVENT) {
                System.out.println (System.getProperty ("java.vendor"));
                System.out.println (System.getProperty ("java.version"));
                System.out.println (System.getProperty ("java.class.version"));
                System.out.println (System.getProperty ("os.name"));
                System.out.println (values);
            }
        }else {
            Vector v;
            Class c = e.target.getClass();
            v = (Vector)values.get(c);
            if (v == null)
                v = new Vector();
            Integer i = new Integer (e.id);
            if (!v.contains (i)) {
                v.addElement (i);
                values.put (c, v);
            }
        }
        return super.handleEvent (e);
    }
}
</PRE>
</DIV>

<P CLASS=para>
An HTML document to display the applet in a browser should look something 
like the following: 

<DIV CLASS=programlisting>
<P>
<PRE>
&lt;APPLET code="compList.class" height=300 width=300&gt;
&lt;/APPLET&gt;
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-APP-C-SECT-2.3">Examining Results</A></h3>

<P CLASS=para>
The results of the program are sent to standard output when you click on 
the Done button. What happens to the output depends on the 
platform. It may be sent to a log file (Internet Explorer), the Java Console 
(Netscape Navigator), or the command line (appletviewer). The following 
is sample output from Internet Explorer 3.0 on a Windows 95 platform. 

<DIV CLASS=programlisting>
<P>
<PRE>
Microsoft Corp.
1.0.2
45.3
Windows 95
{class java.awt.Canvas=[504, 503, 1004, 501, 506, 502, 505, 1005,
401, 402, 403, 404], class java.awt.Choice=[1001, 401, 402, 403,
404], class java.awt.Checkbox=[1001, 402, 401, 403, 404], class 
compList=[504, 503, 501, 506, 502, 505, 1004, 1005], class java.
awt.TextField=[401, 402, 403, 404], class java.awt.List=[701, 
1001, 401, 402, 403, 404, 702], class java.awt.Scrollbar=[602, 
605, 604, 603, 601], class java.awt.TextArea=[401, 402, 403, 404],
class java.awt.Button=[1001, 401, 402, 403, 404]}
</PRE>
</DIV>

<P CLASS=para>
In addition to some identifying information about the run-time environment, 
the program displays a list of classes and the events they passed. The 
integers represent the event constants of the <tt CLASS=literal>Event</tt> 
class; for example, <tt CLASS=literal>Canvas</tt> 
received events with identifiers 504, 503, etc. The events are not sorted, 
so you can see the order in which they were sent. Unfortunately, you have 
to look up these constants in the source code yourself. The class listed as <tt CLASS=literal>compList</tt> 
is the applet itself and shows you the events that the <tt CLASS=literal>Applet</tt> 
class receives. 

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="appc_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="appd_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The Results</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Image Loading</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
